/*
 * WaveViewFrame.java
 * 
 * last update: 14.01.2010 by Stefan Saru
 * 
 * author:	Lucian Ghita Felix(felix@ulise.cs.pub.ro)
 *
 * Obs: N/A
 */


package ide;

import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import middle.*;

import javax.swing.plaf.metal.*;


/**
 * This is the container frame for the WaveForm viewer and some buttons.
 *
 * @version 1.0
 * @author Lucian Ghita Felix
 *
 */
@SuppressWarnings("serial")
public class WaveViewFrame extends JInternalFrame {
	WaveView waveView;
	Project theProject;

	public ZoomInAction zoomInAction = new ZoomInAction();
	public ZoomOutAction zoomOutAction = new ZoomOutAction();
	public RemoveWaveAction removeWaveAction = new RemoveWaveAction();



	public WaveViewFrame(Project theProject) {
		super("vIDE WaveForm Viewer", true, false, true, true);
		vide.spl.showStatus("Creating vIDE wave view frame..");
		this.theProject = theProject;
		//setBackground(UIManager.getColor("control"));
		waveView = new WaveView(theProject);

		final int inset = 100;
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setBounds ( inset, inset, screenSize.width - inset*2, screenSize.height - inset*2 );


		getContentPane().add(waveView, BorderLayout.CENTER);
		getContentPane().add(buildToolbar(), BorderLayout.NORTH);
	}

	private void addB(JToolBar t, AbstractAction a) {
		JButton b = t.add(a);
		b.setText(null);
		b.setToolTipText((String) a.getValue(AbstractAction.LONG_DESCRIPTION));
		b.setMargin(new Insets(0,5,0,5));
		b.setRolloverEnabled(true);
	}
	protected JToolBar buildToolbar() {

		vide.spl.showStatus("Creating WaveFormView toolbar..");
		JToolBar toolBar = new JToolBar();
		toolBar.setFloatable(false);
		toolBar.putClientProperty( "JToolBar.isRollover", Boolean.TRUE );

		addB(toolBar, zoomInAction);
		addB(toolBar, zoomOutAction);
		toolBar.addSeparator();

		addB(toolBar, removeWaveAction);
		toolBar.addSeparator();

		addB(toolBar, theProject.runAction);
		addB(toolBar, theProject.goAction);
		addB(toolBar, theProject.stepAction);
		addB(toolBar, theProject.stopAction);
		vide.spl.showStatus("Done creating toolbar..");
		return toolBar;
	}
	public void saveWaveSet() {
		try {
			JFileChooser chooser = new JFileChooser(".");
			VideFileFilter filter = new VideFileFilter("vws", "Wave Set file");
			chooser.setFileFilter(filter);
			int returnVal = chooser.showSaveDialog(theProject.god);
			if(returnVal != JFileChooser.APPROVE_OPTION) return;
			String name = chooser.getSelectedFile().getName();

			FileOutputStream fostream = new FileOutputStream(name);  // + "." + "vws");
			ObjectOutputStream oostream = new ObjectOutputStream(fostream);
			oostream.writeObject("qwerty");            
			oostream.writeObject(new Integer(((DefaultListModel) waveView.waveNameList.getModel()).getSize()));
			for(int index = 0; index < ((DefaultListModel) waveView.waveNameList.getModel()).getSize();index++){
				oostream.writeObject(((DefaultListModel) waveView.waveNameList.getModel()).getElementAt(index));
				oostream.writeObject((VariableHistory)((DefaultListModel) waveView.waveList.getModel()).getElementAt(index));
			}

			oostream.flush();
			fostream.close();
		} catch (Exception e) {
			//error("Failed saving project with reason:\n" + e.toString());
			System.out.println("IOException : " + e);
		}
	}


	public void openWaveSet() {

		JFileChooser chooser = new JFileChooser(".");
		VideFileFilter filter = new VideFileFilter("vws", "WaveSet file");
		chooser.setFileFilter(filter);
		int returnVal = chooser.showOpenDialog(theProject.god);
		if(returnVal != JFileChooser.APPROVE_OPTION) return;
		String file = chooser.getSelectedFile().getName();


		if (file == null) return; //the user is bored by this enviroment and make mistakes
		if (!(file.endsWith("." + "vwv"))) {
			//error("Could not load wave data.\nA VeriSim wave data file must have \n " + "vwv" + " extension.");
			return;
		}

		try {
			FileInputStream fistream = new FileInputStream(file);  //name + "." + "vwv");
			ObjectInputStream oistream = new ObjectInputStream(fistream);
			if (!(((String) oistream.readObject()).equals("qwerty"))) {
				System.out.println("Could not load wave set : nBad signature.");
				//error("Could not load project.\nBad signature.");
				return;
			}

			waveView.clear();

			int numar = ((Integer)oistream.readObject()).intValue();

			for(int index = 0; index < numar;index++){
				String nameVar = (String)oistream.readObject();
				VariableHistory vh = (VariableHistory)oistream.readObject();
				waveView.addWave(vh,nameVar);
			}

			fistream.close();

		} catch (Exception e) {
			System.out.println("Exception opening WaveSet:" + e.toString());

		}

	}


	class ZoomInAction extends AbstractAction {
		public ZoomInAction() {
			super("ZoomIn");setEnabled(true);
			putValue(SMALL_ICON, new ImageIcon(vide.baseDir + "images/zoomIn.gif"));
			putValue(LONG_DESCRIPTION, "Zoom in");
		}
		public void actionPerformed(java.awt.event.ActionEvent event) {
			zoomOutAction.setEnabled(true); //activeaza ZoomOut
			setEnabled(waveView.ZoomIn());   //daca zoom == maximum ..
		}
	}

	class ZoomOutAction extends AbstractAction {
		public ZoomOutAction() {
			super("ZoomOut");setEnabled(true);
			putValue(SMALL_ICON, new ImageIcon(vide.baseDir + "images/zoomOut.gif"));
			putValue(LONG_DESCRIPTION, "Zoom out");
		}
		public void actionPerformed(java.awt.event.ActionEvent event) {
			zoomInAction.setEnabled(true);   //activeaza ZoomIn
			setEnabled(waveView.ZoomOut());    
		}
	}

	class RemoveWaveAction extends AbstractAction {
		public RemoveWaveAction () {
			super("RemoveWaveActionAction");setEnabled(true);
			putValue(SMALL_ICON, new ImageIcon(vide.baseDir + "images/delWave.gif"));  
			putValue(LONG_DESCRIPTION, "Removes the selected wave");
		}
		public void actionPerformed(java.awt.event.ActionEvent event) {
			waveView.removeWave();
		}
	}

	Object[] reset(){
		return waveView.reset();
	}
}